package cmd

import (
	"github.com/CIPFZ/collector/internal/app"
	"github.com/CIPFZ/collector/pkg/version"

	"github.com/spf13/cobra"
)

// NewRootCommand 创建根命令
func NewRootCommand(collector *app.Collector) *cobra.Command {
	var configFile string
	var debug bool
	var verbose bool

	versionInfo := version.GetVersion()

	rootCmd := &cobra.Command{
		Use:   "collector",
		Short: "A simple and efficient system information collection tool",
		Long: `Collector is a CLI tool for collecting system information, logs, and executing commands.
        
It provides real-time progress tracking, core cancellation, and flexible configuration options.

Examples:
  collector collect                    # Collect using default configuration
  collector collect --live            # Show real-time progress
  collector collect --system-only     # Collect only system information
  collector status                    # Show current status
  collector cancel --all              # Cancel all running tasks`,
		Version: versionInfo.Version,
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			logger := collector.Logger()
			if debug {
				logger.Info("Debug mode enabled")
			}
			if verbose {
				logger.Info("Verbose mode enabled")
			}
		},
	}

	// 全局标志
	rootCmd.PersistentFlags().StringVar(&configFile, "config", "",
		"config file")
	rootCmd.PersistentFlags().BoolVar(&debug, "debug", false,
		"enable debug mode")
	rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false,
		"enable verbose output")

	// 添加子命令
	subCommands := []*cobra.Command{
		// 收集任务下发
		NewCollectCommand(collector),
		// 任务状态
		NewStatusCommand(collector),
		// 任务取消
		NewCancelCommand(collector),
		// 工具版本
		NewVersionCommand(collector),
	}
	for _, cmd := range subCommands {
		rootCmd.AddCommand(cmd)
	}

	return rootCmd
}
